package redpacket.mapper;

import org.apache.ibatis.annotations.*;
import redpacket.model.RedPacket;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface RedPacketMapper {

    @Insert("INSERT INTO red_packet(id, sender_id, total_amount, total_count, remain_count, type, status, expire_time, create_time) " +
            "VALUES(#{id}, #{senderId}, #{totalAmount}, #{totalCount}, #{remainCount}, #{type}, #{status}, #{expireTime}, #{createTime})")
    int insert(RedPacket rp);

    @Select("SELECT * FROM red_packet WHERE id = #{id}")
    RedPacket findById(@Param("id") Long id);

    @Update("UPDATE red_packet SET remain_count = #{remainCount}, status = #{status} WHERE id = #{id}")
    int updateRemainAndStatus(RedPacket rp);

    @Update("UPDATE red_packet SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);

    @Select("SELECT * FROM red_packet WHERE status = 1 AND expire_time < #{now}")
    List<RedPacket> findExpired(@Param("now") LocalDateTime now);
}
